﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Zhaoxi.DeviceMonitor.Models;

namespace Zhaoxi.DeviceMonitor.ViewModels
{
    public class MainViewModel : INotifyPropertyChanged
    {
        public List<AreaItemModel> AreaList { get; set; } =
            new List<AreaItemModel>();

        public ObservableCollection<AlarmItemModel> AlarmList { get; set; } =
            new ObservableCollection<AlarmItemModel>();

        private ushort _value;

        public event PropertyChangedEventHandler? PropertyChanged;

        public ushort Value
        {
            get { return _value; }
            set
            {
                if (_value != value)
                {
                    _value = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Value)));

                    // 判断值是否有效
                    if (value > 100)
                    {
                        Application.Current.Dispatcher.Invoke(() =>
                        {
                            AlarmList.Insert(0, new AlarmItemModel()
                            {
                                Name = "热成像仪",
                                Number = "RCX-01",
                                Area = "BC168机房电机区域",
                                AlarmType = "温度过高",
                                AlarmValue = $"{value}%",
                                AlarmTime = "2023-05-10 20:00",
                                State = 1
                            });
                        });
                    }

                }
            }
        }


        public MainViewModel()
        {
            // 初始化模拟数据 
            for (int i = 0; i < 20; i++)
            {
                AreaList.Add(new AreaItemModel() { Index = i + 1, AreaName = "BC168-皮带机巡检设备" });
            }

            //this.PropertyChanged += MainViewModel_PropertyChanged;

            AlarmList.Add(new AlarmItemModel()
            {
                Name = "热成像仪",
                Number = "RCX-01",
                Area = "BC168机房电机区域",
                AlarmType = "温度过高",
                AlarmValue = "100%",
                AlarmTime = "2023-05-10 20:00",
                State = 0
            });
            AlarmList.Add(new AlarmItemModel()
            {
                Name = "振动仪",
                Number = "RCX-01",
                Area = "BC168机房电机区域",
                AlarmType = "温度过高",
                AlarmValue = "40um",
                AlarmTime = "2023-05-10 20:00",
                State = 0
            });
            AlarmList.Add(new AlarmItemModel
            {
                Name = "测距仪",
                Number = "RCX-01",
                Area = "BC168机房电机区域",
                AlarmType = "异常测距",
                AlarmValue = "20um",
                AlarmTime = "2023-05-10 20:00",
                State = 1
            });
            AlarmList.Add(new AlarmItemModel()
            {
                Name = "巡检小车",
                Number = "RCX-01",
                Area = "BC168机房电机区域",
                AlarmType = "异常噪音",
                AlarmValue = "80",
                AlarmTime = "2023-05-10 20:00",
                State = 0
            });

            // 判断是否是设计模式/运行模式    MvvmLight
            this.Monitor();
        }

        private void MainViewModel_PropertyChanged(object? sender, PropertyChangedEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void Monitor()
        {
            S7.Net.Plc plc = new S7.Net.Plc(S7.Net.CpuType.S7200Smart, "192.168.2.1", 0, 0);
            plc.Open();

            Task.Run(async () =>
            {
                while (true)
                {
                    await Task.Delay(2000);
                    var values = (UInt16[])plc.Read(S7.Net.DataType.DataBlock, 1, 0, S7.Net.VarType.Word, 2);

                    Value = values[0];
                }
            });
        }
    }
}
